Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve performance of memoized methods with arguments by ~50% #50

Merged
merged 1 commit into from
Sep 12, 2024

Conversation

CKolkey
Copy link
Contributor

@CKolkey CKolkey commented Sep 12, 2024

Based on the information in this twitter thread, I believe we can improve the hash-lookup times of memoized method calls with arguments substantially, with only a small change.

https://threadreaderapp.com/thread/1831412716533379235.html

Running benchmark.rb prior to change:

Warming up --------------------------------------
        test_no_args   526.602k i/100ms
Calculating -------------------------------------
        test_no_args      5.172M (± 1.8%) i/s -     26.330M in   5.092880s
Calculating -------------------------------------
        test_no_args     4.000k memsize (     0.000  retained)
                       100.000  objects (     0.000  retained)
                         0.000  strings (     0.000  retained)
Warming up --------------------------------------
      test_with_args   221.217k i/100ms
Calculating -------------------------------------
      test_with_args      2.203M (± 5.6%) i/s -     11.061M in   5.045789s
Calculating -------------------------------------
      test_with_args    12.000k memsize (     0.000  retained)
                       300.000  objects (     0.000  retained)
                         0.000  strings (     0.000  retained)

And after:

Warming up --------------------------------------
        test_no_args   529.096k i/100ms
Calculating -------------------------------------
        test_no_args      5.247M (± 1.3%) i/s -     26.455M in   5.042306s
Calculating -------------------------------------
        test_no_args     4.000k memsize (     0.000  retained)
                       100.000  objects (     0.000  retained)
                         0.000  strings (     0.000  retained)
Warming up --------------------------------------
      test_with_args   331.604k i/100ms
Calculating -------------------------------------
      test_with_args      3.471M (± 0.8%) i/s -     17.575M in   5.064376s
Calculating -------------------------------------
      test_with_args    12.000k memsize (     0.000  retained)
                       300.000  objects (     0.000  retained)
                         0.000  strings (     0.000  retained)

The test_no_args case is unaffected (as expected) but the test_with_args case sees a very significant speedup.

Running benchmark.rb prior to change:
```
Warming up --------------------------------------
        test_no_args   526.602k i/100ms
Calculating -------------------------------------
        test_no_args      5.172M (± 1.8%) i/s -     26.330M in   5.092880s
Calculating -------------------------------------
        test_no_args     4.000k memsize (     0.000  retained)
                       100.000  objects (     0.000  retained)
                         0.000  strings (     0.000  retained)
Warming up --------------------------------------
      test_with_args   221.217k i/100ms
Calculating -------------------------------------
      test_with_args      2.203M (± 5.6%) i/s -     11.061M in   5.045789s
Calculating -------------------------------------
      test_with_args    12.000k memsize (     0.000  retained)
                       300.000  objects (     0.000  retained)
                         0.000  strings (     0.000  retained)
```

After:
```
Warming up --------------------------------------
        test_no_args   529.096k i/100ms
Calculating -------------------------------------
        test_no_args      5.247M (± 1.3%) i/s -     26.455M in   5.042306s
Calculating -------------------------------------
        test_no_args     4.000k memsize (     0.000  retained)
                       100.000  objects (     0.000  retained)
                         0.000  strings (     0.000  retained)
Warming up --------------------------------------
      test_with_args   331.604k i/100ms
Calculating -------------------------------------
      test_with_args      3.471M (± 0.8%) i/s -     17.575M in   5.064376s
Calculating -------------------------------------
      test_with_args    12.000k memsize (     0.000  retained)
                       300.000  objects (     0.000  retained)
                         0.000  strings (     0.000  retained)
```
@CKolkey CKolkey changed the title Update array hash keys to integers Improve performance of memoized methods with arguments Sep 12, 2024
@CKolkey CKolkey changed the title Improve performance of memoized methods with arguments Improve performance of memoized methods with arguments by ~50% Sep 12, 2024
@tycooon tycooon merged commit 0bd5b5a into tycooon:master Sep 12, 2024
6 checks passed
@tycooon
Copy link
Owner

tycooon commented Sep 12, 2024

This looks good, thanks for the patch!

AlexWayfer added a commit to AlexWayfer/alt_memery that referenced this pull request Sep 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants